package com.zhs.gccs.utils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.util.Log;

import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.Poi;
import com.amap.api.navi.AmapNaviPage;
import com.amap.api.navi.AmapNaviParams;
import com.amap.api.navi.AmapNaviType;
import com.amap.api.navi.AmapPageType;
import com.zhs.gccs.model.LocationBean;

import java.io.File;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;

public class NavUtils {
    //http://www.manongjc.com/detail/62-lmmgmxvbqllbfvw.html
    public static void invokingBD(Activity content, String address) {
        //  com.baidu.BaiduMap这是高德地图的包名
        //调起百度地图客户端try {
        Intent intent = null;
        try {
            String uri = "intent://map/direction?origin=latlng:0,0|name:我的位置&destination=" + address   + "&mode=driving&referer=Autohome|GasStation#Intent;scheme=bdapp;package=com.baidu.BaiduMap;end";
            intent = Intent.getIntent(uri);
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
        if (isInstalled(content,"com.baidu.BaiduMap")) {
            content.startActivity(intent); //启动调用
            Log.e("GasStation", "百度地图客户端已经安装");
        } else {
            ToastUtils.toast("没有安装百度地图客户端");
        }
    }

    public static void invokingGD(Activity content, String address) {
        //  com.autonavi.minimap这是高德地图的包名
        Intent intent = new Intent("android.intent.action.VIEW", android.net.Uri.parse("androidamap://navi?sourceApplication=应用名称&lat=" + "&dev=0"));
        intent.setPackage("com.autonavi.minimap");
        intent.setData(Uri.parse("androidamap://poi?sourceApplication=softname&keywords=" + address));
        if (isInstalled(content,"com.autonavi.minimap")) {
            content.startActivity(intent);
            Log.e("GasStation", "高德地图客户端已经安装");
        } else {
            ToastUtils.toast("没有安装高德地图客户端");
        }
    }

    public static void startGdNav(Context context, LocationBean startLoc,LocationBean endLoc){
        //起点
        Poi start = new Poi(startLoc.getAddress(), new LatLng(startLoc.getLat(),startLoc.getLon()), "B000A28DAE");
        //途经点
        List<Poi> poiList = new ArrayList();
//        poiList.add(new Poi("故宫", new LatLng(39.918058,116.397026), "B000A8UIN8"));
        //终点
        Poi end = new Poi(endLoc.getAddress(), new LatLng(endLoc.getLat(),endLoc.getLon()), "B000A816R6");
        // 组件参数配置
        AmapNaviParams params = new AmapNaviParams(start, poiList, end, AmapNaviType.DRIVER, AmapPageType.ROUTE);
        // 启动组件
        AmapNaviPage.getInstance()
                .showRouteActivity(context, params, null);
    }



//    private void invokingTencentMap() {
//        LatLng startPoint = BD2GCJ(new LatLng(mLatitude, mLongitude));//坐标转换
//        LatLng endPoint = BD2GCJ(new LatLng(31.220567, 121.395174));//坐标转换
//        StringBuffer sb = new StringBuffer("qqmap://map/routeplan")
//                .append("?type=drive")
//                .append("&from=我的位置")
//                .append("&fromcoord=").append(startPoint.latitude).append(",").append(startPoint.longitude)
//                .append("&to=长宁图书馆")
//                .append("&tocoord=").append(endPoint.latitude).append(",").append(endPoint.longitude)
//                .append("&referer=你的key");
//        Intent intent = new Intent();
//        intent.setAction(Intent.ACTION_VIEW);
//        intent.setData(Uri.parse(sb.toString()));
//        startActivity(intent);
//    }
    /**
     * 判断是否安装目标应用
     *
     * @param packageName 目标应用安装后的包名
     * @return 是否已安装目标应用
     */
    private static boolean isInstallByread(String packageName) {
        return new File("/data/data/" + packageName).exists();
    }

    /**
     * 检测所选地图是否安装
     * String[] packageNames = {"com.baidu.BaiduMap", "com.autonavi.minimap", "com.tencent.map"};
     */
    private static boolean isInstalled(Activity content,String packageName) {
        PackageManager manager = content.getPackageManager();
        List<PackageInfo> installedPackages = manager.getInstalledPackages(0);
        if (installedPackages != null) {
            for (PackageInfo info : installedPackages) {
                if (info.packageName.equals(packageName))
                    return true;
            }
        }
        return false;
    }
}
